package com.sx.water.modules.sever.impl;

import com.sx.water.datasourcse.DataSourceNames;
import com.sx.water.datasourcse.annnotiaon.DataSource;
import com.sx.water.modules.dao.IntegerTimeRainingDao;
import com.sx.water.modules.entity.IntegerTimeRainingBo;
import com.sx.water.modules.sever.SurfaceRainfallServer;
import com.sx.water.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

/**
 * @description: 气象接口实现类
 * @author: taoxi
 * @create: 2018-11-01 17:18
 **/
@Service
public class SurfaceRainfallServerImpl implements SurfaceRainfallServer {
    @Autowired
    IntegerTimeRainingDao integerTimeRainingDao;


    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public String selectMaxtime() {
        String timeSuffix=getTbaleSuffix();
         String time =integerTimeRainingDao.selectMaxtime(timeSuffix);

        return time;
    }

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public List<Map<String,Object>> selectSurfaceRainFall(IntegerTimeRainingBo bo) {

        return integerTimeRainingDao.selectSurfaceRainFall(bo);
    }

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public Map<String, Integer> selectGetCount() {
        return integerTimeRainingDao.selectGetCount();
    }

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public List<Map<String, Object>> selectGetRainInfo(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectGetRainInfo(bo);
    }

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public List<Map<String, Object>> selectGetRainInfo2(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectGetRainInfo2(bo);
    }


    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public Map<String, List<Map<String, Object>>> selectRainStrong(IntegerTimeRainingBo bo) {

        Map<String,List<Map<String, Object>>> map=new HashMap<String,List<Map<String, Object>>>();
        //小时
        List<Map<String, Object>> listHour=new ArrayList<>();
        listHour=integerTimeRainingDao.selectRainStrong(bo);
        for(int m=0;m<listHour.size();m++){
            listHour.get(m).put("tyCh","1");
        }
        map.put("hour",listHour);
        //分钟（取第一条）
        List<Map<String, Object>> listMinit=new ArrayList<>();
        listMinit=integerTimeRainingDao.selectRainViwe(bo);
        List<Map<String, Object>> listMinit2=new ArrayList<>();
        if(listMinit.size()>0 && listMinit != null){
            listMinit.get(0).put("tyCh","1");
            listMinit2.add(listMinit.get(0));
        }

        map.put("minit",listMinit2);

        return map;
    }

    /**
     * 获取最新表后缀，根据当前时间
     * @return yyyymm
     */
    public  String getTbaleSuffix(){
        StringBuffer stringBuffer=new StringBuffer();
        Calendar calendar=Calendar.getInstance();
        DecimalFormat df=new DecimalFormat("00");
        //Date date=new Date();
        //判断现在是否是本月最后一天
        if(DateUtils.isLastDayOfMonth(null)){
         //是，判断当前时间 是否超过 20点
          if(calendar.get(Calendar.HOUR_OF_DAY) <= 20){
              stringBuffer.append(calendar.get(Calendar.YEAR)).append(df.format( calendar.get(Calendar.MONTH)+1));
          }else{
              calendar.add(Calendar.MONTH,1);
              stringBuffer.append(calendar.get(Calendar.YEAR)).append(df.format( calendar.get(Calendar.MONTH)+1));
          }
        }else {
            stringBuffer.append(calendar.get(Calendar.YEAR)).append(df.format( calendar.get(Calendar.MONTH)+1));
        }

        return stringBuffer.toString();
    }

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public Map<String, List<Map<String, Object>>> selectFaceRainByCountry(IntegerTimeRainingBo bo, int num) {
        Map<String, List<Map<String, Object>>> stringListMap= new HashMap<>();

        //当传值不是时间段时
        if(num==1){
            //县 逐小时 面雨量     18103121
            List<Map<String, Object>> selectFaceRainOnHour  =integerTimeRainingDao.selectFaceRainOnHour(bo);
            //县 逐小时（未满小时） 面雨量   201811151726   1
            List<Map<String, Object>> selectFaceRainOnMinute  =integerTimeRainingDao.selectFaceRainOnMinute(bo);
            for(int m=0;m<selectFaceRainOnHour.size();m++){
                selectFaceRainOnHour.get(m).put("tyCh","1");
            }
            for(int m=0;m<selectFaceRainOnMinute.size();m++){
                selectFaceRainOnMinute.get(m).put("tyCh","1");
            }
            stringListMap.put("hour",selectFaceRainOnHour);
            stringListMap.put("mint",selectFaceRainOnMinute);
        }else {
            //县 逐小时 面雨量     18103121
            List<Map<String, Object>> selectFaceRainOnHour  =integerTimeRainingDao.selectFaceRainOnHour(bo);
            //县 逐小时（未满小时） 面雨量   201811151726   1
            List<Map<String, Object>> selectFaceRainOnMinute  =new ArrayList<>();
            for(int m=0;m<selectFaceRainOnHour.size();m++){
                selectFaceRainOnHour.get(m).put("tyCh","1");
            }
            stringListMap.put("hour",selectFaceRainOnHour);
            stringListMap.put("mint",selectFaceRainOnMinute);
        }
        return stringListMap;
    }

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public Map<String, List<Map<String, Object>>> selectFaceRainByStation(IntegerTimeRainingBo bo, int num) {
        Map<String, List<Map<String, Object>>> stringListMap= new HashMap<>();
        //type 待判断

        //当传值不是时间段时
        if(num==1){
            //县 逐小时 面雨量     18103121
            List<Map<String, Object>> selectFaceRainOnHour  =integerTimeRainingDao.selectFaceRainOnHourByStation(bo);
            //县 逐小时（未满小时） 面雨量   201811151726   1
            List<Map<String, Object>> selectFaceRainOnMinute  =integerTimeRainingDao.selectFaceRainOnMinuteByStation(bo);
            for(int m=0;m<selectFaceRainOnHour.size();m++){
                selectFaceRainOnHour.get(m).put("tyCh","1");
            }
            for(int m=0;m<selectFaceRainOnMinute.size();m++){
                selectFaceRainOnMinute.get(m).put("tyCh","1");
            }
            stringListMap.put("hour",selectFaceRainOnHour);
            stringListMap.put("mint",selectFaceRainOnMinute);
        }else {
            //县 逐小时 面雨量     18103121
            List<Map<String, Object>> selectFaceRainOnHour  =integerTimeRainingDao.selectFaceRainOnHourByStation(bo);
            //县 逐小时（未满小时） 面雨量   201811151726   1
            List<Map<String, Object>> selectFaceRainOnMinute  =new ArrayList<>();
            for(int m=0;m<selectFaceRainOnHour.size();m++){
                selectFaceRainOnHour.get(m).put("tyCh","1");
            }

            stringListMap.put("hour",selectFaceRainOnHour);
            stringListMap.put("mint",selectFaceRainOnMinute);
        }


        return stringListMap;
    }



    //===========定时任务

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public List<Map<String, Object>> selectGetRainInfoQuartz(IntegerTimeRainingBo bo) {
        return integerTimeRainingDao.selectGetRainInfoQuartz(bo);
    }

    @DataSource(name = DataSourceNames.FIRST)
    @Override
    public List<Map<String, Object>> selectRainStationInfo() {
        return integerTimeRainingDao.selectRainStationInfo();
    }
}
